package org.shirdrn.document.preprocessing.driver.common;

import java.io.File;
import java.util.Properties;

import org.shirdrn.document.preprocessing.api.Context;
import org.shirdrn.document.preprocessing.api.ProcessorType;
import org.shirdrn.document.preprocessing.api.constants.ConfigKeys;
import org.shirdrn.document.preprocessing.core.common.Component;
import org.shirdrn.document.preprocessing.core.utils.PreprocessingUtils;
import org.shirdrn.document.preprocessing.core.utils.ReflectionUtils;
import org.springframework.web.socket.WebSocketSession;

public abstract class AbstractDocumentProcessorDriver {
	public abstract void preprocess(String uid,WebSocketSession session,Properties p);
	protected void run(Component[] chain) {
		for (int i = 0; i < chain.length - 1; i++) {
			Component current = chain[i];
			Component next = chain[i + 1];
			current.setNext(next);
		}
		
		for (Component componennt : chain) {
			//step 7  断开连接之后如果分类器模型没有建立完的话,后面的步骤就不要进行了
			if(componennt.isConnected() || componennt.isSessionNull()){
				componennt.fire();
			}
		}
	}
	public Context newContext(ProcessorType type, String config) {
		return PreprocessingUtils.newContext(type, config);
	}
	//overload
	public Context newContext(ProcessorType type, Properties p) {
		return PreprocessingUtils.newContext(type, p);
	}
	
	public static void start(Class<? extends AbstractDocumentProcessorDriver> driverClass,String uid,WebSocketSession session,Properties p) {
		AbstractDocumentProcessorDriver driver = ReflectionUtils.newInstance(driverClass);
		Properties newPros = (Properties) p.clone();
		String []keys = {ConfigKeys.DATASET_FEATURE_TERM_VECTOR_FILE,ConfigKeys.DATASET_LABEL_VECTOR_FILE,ConfigKeys.DATASET_TRAIN_SVM_VECTOR_OUTPUT_DIR,ConfigKeys.DATASET_TEST_SVM_VECTOR_OUTPUT_DIR};
		//train test 都会执行
		for(String key:keys){
			String path = p.getProperty(key);
			File f = new File(path,uid);
			newPros.setProperty(key, f.getAbsolutePath());
		}	
		driver.preprocess( uid, session,newPros);
	}
}
